package com.qdu.spring.blog.controller.page;

import com.qdu.spring.blog.util.MyConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 页面跳转控制器
 */
@Controller
public class PageController {
    //测试
    @GetMapping("/fileIndex")
    public String fileIndex(){
        return "fileIndex";
    }

    //首页
    @GetMapping("/index.html")
    public String index()
    {
        return "index";
    }

    //登录页面
    @GetMapping(MyConstant.LOGIN_PATH)
    public String login()
    {
        return "login";
    }

    //退出
    @GetMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response)
    {
        // 删除cookie，取消自动登录
        request.getSession().invalidate();
        // 立即删除
        int maxAge = 0;

        Cookie loginNameCookie = new Cookie("name", "");
        loginNameCookie.setMaxAge(maxAge);
        loginNameCookie.setPath("/");
        response.addCookie(loginNameCookie);

        Cookie loginEmailCookie = new Cookie("email", "");
        loginEmailCookie.setMaxAge(maxAge);
        loginEmailCookie.setPath("/");
        response.addCookie(loginEmailCookie);

        Cookie passwordCookie = new Cookie("password", "");
        passwordCookie.setMaxAge(maxAge);
        passwordCookie.setPath("/");
        response.addCookie(passwordCookie);

        return "redirect:" + MyConstant.LOGIN_PATH;
    }
}
